//===================================================================
// 项目名 : Hbt.WebApi
// 文件名 : HbtPlantController.cs
// 创建者 : Claude
// 创建时间: 2024-12-01
// 版本号 : V0.0.1
// 描述: 工厂控制器
//===================================================================

using Hbt.Application.Dtos.Logistics.Material.Master;
using Hbt.Application.Services.Logistics.Material.Master;
using Hbt.Common.Models;
using Hbt.Infrastructure.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hbt.WebApi.Controllers.Logistics.Material.Master
{
    /// <summary>
    /// 工厂控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class HbtPlantController : HbtBaseController
    {
        private readonly IHbtPlantService _plantService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="plantService">工厂服务</param>
        /// <param name="logger">日志服务</param>
        /// <param name="currentUser">当前用户服务</param>
        /// <param name="localization">本地化服务</param>
        public HbtPlantController(
            IHbtPlantService plantService,
            IHbtLogger logger,
            IHbtCurrentUser currentUser,
            IHbtLocalizationService localization) : base(logger, currentUser, localization)
        {
            _plantService = plantService ?? throw new ArgumentNullException(nameof(plantService));
        }

        /// <summary>
        /// 获取工厂分页列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>返回分页结果</returns>
        [HttpGet("list")]
        [HbtPerm("logistics:material:master:plant:list")]
        public async Task<HbtApiResult<HbtPagedResult<HbtPlantDto>>> GetListAsync([FromQuery] HbtPlantQueryDto query)
        {
            try
            {
                var result = await _plantService.GetListAsync(query);
                return HbtApiResult<HbtPagedResult<HbtPlantDto>>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<HbtPagedResult<HbtPlantDto>>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 根据ID获取工厂详情
        /// </summary>
        /// <param name="id">工厂ID</param>
        /// <returns>返回工厂详情</returns>
        [HttpGet("{id}")]
        [HbtPerm("logistics:material:master:plant:list")]
        public async Task<HbtApiResult<HbtPlantDto>> GetByIdAsync(long id)
        {
            try
            {
                var result = await _plantService.GetByIdAsync(id);
                return HbtApiResult<HbtPlantDto>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<HbtPlantDto>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 根据工厂编码获取工厂详情
        /// </summary>
        /// <param name="plantCode">工厂编码</param>
        /// <returns>返回工厂详情</returns>
        [HttpGet("code/{plantCode}")]
        [HbtPerm("logistics:material:master:plant:list")]
        public async Task<HbtApiResult<HbtPlantDto>> GetByPlantCodeAsync(string plantCode)
        {
            try
            {
                var result = await _plantService.GetByPlantCodeAsync(plantCode);
                return HbtApiResult<HbtPlantDto>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<HbtPlantDto>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 创建工厂
        /// </summary>
        /// <param name="input">创建对象</param>
        /// <returns>返回创建结果</returns>
        [HttpPost]
        [HbtPerm("logistics:material:master:plant:create")]
        public async Task<HbtApiResult<long>> CreateAsync([FromBody] HbtPlantCreateDto input)
        {
            try
            {
                var result = await _plantService.CreateAsync(input);
                return HbtApiResult<long>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<long>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新工厂
        /// </summary>
        /// <param name="input">更新对象</param>
        /// <returns>返回更新结果</returns>
        [HttpPut]
        [HbtPerm("logistics:material:master:plant:edit")]
        public async Task<HbtApiResult<bool>> UpdateAsync([FromBody] HbtPlantUpdateDto input)
        {
            try
            {
                var result = await _plantService.UpdateAsync(input);
                return HbtApiResult<bool>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<bool>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 删除工厂
        /// </summary>
        /// <param name="id">工厂ID</param>
        /// <returns>返回删除结果</returns>
        [HttpDelete("{id}")]
        [HbtPerm("logistics:material:master:plant:delete")]
        public async Task<HbtApiResult<bool>> DeleteAsync(long id)
        {
            try
            {
                var result = await _plantService.DeleteAsync(id);
                return HbtApiResult<bool>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<bool>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 批量删除工厂
        /// </summary>
        /// <param name="ids">工厂ID集合</param>
        /// <returns>返回删除结果</returns>
        [HttpDelete("batch")]
        [HbtPerm("logistics:material:master:plant:delete")]
        public async Task<HbtApiResult<bool>> BatchDeleteAsync([FromBody] long[] ids)
        {
            try
            {
                var result = await _plantService.BatchDeleteAsync(ids);
                return HbtApiResult<bool>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<bool>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新工厂状态
        /// </summary>
        /// <param name="input">状态更新对象</param>
        /// <returns>返回更新结果</returns>
        [HttpPut("status")]
        [HbtPerm("logistics:material:master:plant:edit")]
        public async Task<HbtApiResult<bool>> UpdateStatusAsync([FromBody] HbtPlantStatusDto input)
        {
            try
            {
                var result = await _plantService.UpdateStatusAsync(input);
                return HbtApiResult<bool>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<bool>.Error(ex.Message);
            }
        }

        /// <summary>
        /// 导入工厂数据
        /// </summary>
        /// <param name="file">Excel文件</param>
        /// <returns>导入结果</returns>
        [HttpPost("import")]
        [HbtPerm("logistics:material:master:plant:import")]
        public async Task<IActionResult> ImportAsync(IFormFile file)
        {
            using var stream = file.OpenReadStream();
            var (success, fail) = await _plantService.ImportAsync(stream, "工厂信息");
            return Success(new { success, fail }, _localization.L("Plant.Import.Success"));
        }

        /// <summary>
        /// 导出工厂数据
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>Excel文件或zip文件</returns>
        [HttpGet("export")]
        [HbtPerm("logistics:material:master:plant:export")]
        public async Task<IActionResult> ExportAsync([FromQuery] HbtPlantQueryDto query)
        {
            var result = await _plantService.ExportAsync(query, "Plant");
            var contentType = result.fileName.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)
                ? "application/zip"
                : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            // 只在 filename* 用 UTF-8 编码，filename 用 ASCII
            var safeFileName = System.Text.Encoding.ASCII.GetString(System.Text.Encoding.ASCII.GetBytes(result.fileName));
            Response.Headers["Content-Disposition"] = $"attachment; filename*=UTF-8''{Uri.EscapeDataString(result.fileName)}";
            return File(result.content, contentType, result.fileName);
        }

        /// <summary>
        /// 获取工厂导入模板
        /// </summary>
        /// <returns>Excel模板文件</returns>
        [HttpGet("template")]
        [HbtPerm("logistics:material:master:plant:export")]
        public async Task<IActionResult> GetTemplateAsync()
        {
            var result = await _plantService.GetTemplateAsync("工厂信息");
            return File(result.content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", result.fileName);
        }

        /// <summary>
        /// 获取工厂选项列表
        /// </summary>
        /// <returns>返回工厂选项列表</returns>
        [HttpGet("options")]
        [HbtPerm("logistics:material:master:plant:list")]
        public async Task<HbtApiResult<List<HbtSelectOption>>> GetOptionsAsync()
        {
            try
            {
                var result = await _plantService.GetOptionsAsync();
                return HbtApiResult<List<HbtSelectOption>>.Success(result);
            }
            catch (Exception ex)
            {
                return HbtApiResult<List<HbtSelectOption>>.Error(ex.Message);
            }
        }


    }
}
